﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Server
{
    public partial class Baocao : Form
    {
        public Baocao()
        {
            InitializeComponent();
        }

        #region "Hàm"
        /// <summary>
        /// Load danh sách account
        /// </summary>
        void LoadAccount()
        {
            CXuLyXML xulyxml = new CXuLyXML();
            List<CAccount> arr = xulyxml.LayDanhSachAccount();
            foreach (var i in arr)
            {
                listAccount.Items.Add(i.Taikhoan);
            }
            lblCountAccount.Text = "Count: " + arr.Count.ToString() + " account";
            cboNgay.SelectedIndex = 0;
        }
        /// <summary>
        /// Load danh sách báo cáo chung
        /// </summary>
        void LoadBaoCaoChung(DateTime _date)
        {
            treeChung.Nodes.Clear();
            CXuLyXML xulyxml = new CXuLyXML();
            CReport report = xulyxml.LayDanhSachBaoCao(_date);
            TreeNode Node = CreateNode(report);
            treeChung.Nodes.Add(Node);
            treeChung.ExpandAll();
        }
        /// <summary>
        /// Create node
        /// </summary>
        /// <param name="report"></param>
        /// <returns></returns>
        private TreeNode CreateNode(CReport report)
        {
            TreeNode node = new TreeNode(report.Ngaybaocao.ToShortDateString());
            node.Nodes.AddRange(new TreeNode[] 
            { 
               new TreeNode("Sản phẩm bán nhiều nhất: " + report.Sanphambannhieunhat.Ten+ ", Số lượng: " + report.Soluongsanphambannhieunhat),
               new TreeNode ("Sản phẩm bán ít nhất: " + report.Sanphambanitnhat.Ten + ", Số lượng: " + report.Soluongsanphambanitnhat),
               new TreeNode ("Tổng doanh thu: " + string.Format("{0:c}",report.Tongdoanhthu).Replace("$","").Replace(".00","") +" VNĐ"),
               CreateNode(report.Listsanphamban)
            });
            return node;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="list"></param>
        /// <returns></returns>
        private TreeNode CreateNode(List<CProduct> list)
        {

            TreeNode root = new TreeNode("Danh sách sản phẩm bán được");
            TreeNode[] arr = new TreeNode[list.Count];

            for (int i = 0; i < list.Count; i++)
            {
                TreeNode node = new TreeNode(list[i].Ten + ", Số lượng: " + list[i].Soluongbanduoc + ", Thành tiền: " + string.Format("{0:c}", list[i].Doanhthu).Replace("$", "").Replace(".00", "") + " VNĐ");
                arr[i] = node;
            }

            root.Nodes.AddRange(arr);
            return root;
        }
        /// <summary>
        /// 
        /// </summary>
        void LoadBaoCaoRieng(string account, DateTime _date)
        {
            if (listAccount.SelectedItems.Count != 0)
            {
                treeRieng.Nodes.Clear();
                CXuLyXML xulyxml = new CXuLyXML();
                CReport report = xulyxml.LayDanhSachBaoCao(_date, account);
                if (report != null)
                {
                    TreeNode Node = CreateNode(report);
                    treeRieng.Nodes.Add(Node);
                    treeRieng.ExpandAll();
                }
            }
        }
        #endregion

        #region "Event"
        private void Baocao_Load(object sender, EventArgs e)
        {
            LoadAccount();
            LoadBaoCaoChung(DateTime.Now);
            LoadNgay();
        }
        /// <summary>
        /// Load ngaỳ
        /// </summary>
        private void LoadNgay()
        {
            CXuLyXML xulyxml = new CXuLyXML();
            List<DateTime> list = xulyxml.LayCacNgayBanHang();
            foreach (var i in list)
            {
                cboNgay.Items.Add(i.ToShortDateString());
            }
        }
        /// <summary>
        /// Làm mới dữ liệu
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Refresh_Click(object sender, EventArgs e)
        {
            listAccount.Items.Clear();
            LoadAccount();
            LoadBaoCaoChung(DateTime.Now);
        }
        /// <summary>
        /// Thoát form
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Exit_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        /// <summary>
        /// Chọn account để báo cáo
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void listAccount_SelectedValueChanged(object sender, EventArgs e)
        {
            if (listAccount.SelectedItems.Count != 0)
            {
                treeRieng.Nodes.Clear();
                string _user = listAccount.SelectedItem.ToString();
                LoadBaoCaoRieng(_user, DateTime.Now);
            }
        }
        private void cboNgay_SelectedIndexChanged(object sender, EventArgs e)
        {
            DateTime _date = DateTime.Now;
            if (cboNgay.SelectedIndex > 0)
            {
                string _strdatetime = cboNgay.SelectedItem.ToString();
                _date = CDateTime.DateTimeParse(_strdatetime);
                
            }

            LoadBaoCaoChung(_date);
            if (listAccount.SelectedItems.Count != 0)
            {
                string _user = listAccount.SelectedItem.ToString();
                LoadBaoCaoRieng(_user, _date);
            }
        }
        #endregion

       

    }
}
